<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>基础接口</title>
</head>
<body>
<?php

define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
    $wechatObj->valid();
}else{
    $wechatObj->responseMsg();
}

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            header('content-type:text');
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
      
           $LX_TYPE = trim($postObj->MsgType);

           switch($LX_TYPE){
			   
			   case "text":
			   $result = $this->receiveText($postObj);
			   break;
			   
			   case "event":
			   $result = $this->receiveEvent($postObj);
			   break;
		   }	
    
            echo $result;	
          
        }else{
            echo "";
            exit;
        }
    }
	
    
    
	
  //扫码事件 EventKey存在但拿不到。。。
  
    private function receiveEvent($object){
		
		$content ="";
		
		$EVENT_TYPE = trim($object->Event);
		
		switch($EVENT_TYPE){
			
			case "subscribe":
			$content = "欢迎关注溯源平台";
			$result = $this->transmitText($object,$content);
			break;
			
			
				
			
			
			 case "scancode_waitmsg":
			 $hfURL = str_replace("http://weixin.qq.com/r/sjh9ZeHEM7xMraUG922g?add=","",$object->ScanCodeInfo->ScanResult);
			 $content = array();
			 $content[] = array("Title"=>"蜂产品溯源查询",
			                    "Description"=>"溯源部分类容",
								"PicUrl"=>"http://img1.tbcdn.cn/tfscom/i2/TB1KtFWHFXXXXccXXXXXXXXXXXX_!!0-item_pic.jpg_300x300.jpg",
								"Url"=>"http://beebeebee.net");
         
             $result = $this->transmitNews($object, $content);
             break;
	
		}
		
		return $result;
	}
	
    
    //测试用的回复信息
	private function receiveText($object){
		
		$content ="您发送的消息是".$object->Content;
		
		$result =$this->transmitText($object,$content);
		
		return $result;
	}
	//文本回复
	private function transmitText($object,$content){
		
		$textTpl ="<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[text]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        </xml>";
		
		$result = sprintf($textTpl,$object->FromUserName,$object->ToUserName,time(),$content);
		
		return $result;
	}
	
    //scan回复
	

	
	//扫码提示
   	private function transmitNews($object,$arr_item){
		
        if(!is_array($arr_item)){
		  
		      return;
	    }
	  
	    $itemTpl = "<item>
		            <Title><![CDATA[%s]]></Title>
					<Description><![CDATA[%s]]></Description>
					<PicUrl><![CDATA[%s]]></PicUrl>
					<Url><![CDATA[%s]]></Url>
		            </item>";
					
		$item_str = "";
		
		foreach($arr_item as $item)
		
		$item_str.=sprintf($itemTpl,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']);
	
        	$newsTpl ="<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[news]]></MsgType>
                        <Content><![CDATA[]]></Content>
						<ArticleCount>%s</ArticleCount>
						<Articles>$item_str</Articles>
                        </xml>";
						
			$result = sprintf($newsTpl,$object->FromUserName,$object->ToUserName,time(),count($arr_item));
			
			return $result;
	}
    
   
	
}


?>

</body>
</html>
